﻿#region Usings
using System;
using System.Collections.Generic;
using System.Linq;
using PloobsEngine.Cameras;
using PloobsEngine.Light;
using PloobsEngine.SceneControl;
using PloobsEngine.Modelo;
using PloobsEngine.Modelo.Animation;

#endregion

namespace PloobsEngine.Material
{    
    public abstract class IShader
    {        
        protected IList<ILight> lights;
        protected IObject obj;        
        protected bool firstTime = true;
        private ParcialShader partialShader = null;
        protected float shaderId = 0;
        protected IDepthExtractor depthExtractor;     

        public abstract MaterialType MaterialType
        {
            get;
        }

        /// <summary>
        /// Used to Recover the object in PostProcces
        /// between 0 and 0.9f it will be illuminated, 
        /// bigger than 0.9 it wont be
        /// </summary>
        public float ShaderId
        {
            get { return shaderId; }
            set { shaderId = value; }
        }

        public ParcialShader PartialShader
        {
            get { return partialShader; }
            set { partialShader = value; }
        }

        public IShader()
        {            
        }

        public IDepthExtractor DepthExtractor
        {
            get
            {
                return depthExtractor;
            }
            set
            {
                this.depthExtractor = value;
            }
        }

        public virtual void Initialize()
        {            
            if (partialShader != null)
                partialShader.Initialize();
        }
        /// <summary>
        /// Called Once only, before all draws
        /// </summary>
        /// <param name="ent"></param>
        /// <param name="lights"></param>
        public virtual void PreUpdate(IObject ent, IList<ILight> lights) { }
        

        public virtual void Update(IObject ent, IList<ILight> lights)
        {
            if (firstTime)
            {             
                PreUpdate(ent, lights);
                firstTime = false;
            }

            this.obj = ent;
            this.lights = lights;

            if (partialShader != null)
                partialShader.Update(ent, lights);           

        }

        public virtual void Serialize(System.Xml.XmlTextWriter textWriter)
        {
        }
        public virtual void DeSerialize(System.Xml.XmlNode node, EngineStuff engine, IAnimatedController con)
        {
        }

        public virtual void PreDrawPhase(IWorld world,IModelo modelo, IRenderHelper render, ICamera cam)
        {
            if (partialShader != null && partialShader.ShaderParcialFuncion == ShaderParcialFuncion.PRE_DRAW)
                partialShader.Draw(modelo, render, cam, lights);
        }

        public virtual void PosDrawPhase(IModelo modelo, IRenderHelper render, ICamera cam , IList<ILight> lights)
        {
            if (partialShader != null && partialShader.ShaderParcialFuncion == ShaderParcialFuncion.POS_DRAW)
                partialShader.Draw(modelo, render, cam, lights);
        }

        public virtual void Draw(IModelo modelo, IRenderHelper render, ICamera cam)
        {
            if (partialShader != null && partialShader.ShaderParcialFuncion == ShaderParcialFuncion.DRAW)
                partialShader.Draw(modelo, render, cam,lights);
        }
                

    }
}
